package com.liesheng.haylou.ui.device;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.view.View;
import android.view.WindowManager;

import androidx.databinding.DataBindingUtil;

import com.liesheng.haylou.R;
import com.liesheng.haylou.base.BaseActivity;
import com.liesheng.haylou.base.BaseFunActivity;
import com.liesheng.haylou.base.BaseVm;
import com.liesheng.haylou.bluetooth.BleDataInfo;
import com.liesheng.haylou.bluetooth.BleManager;
import com.liesheng.haylou.databinding.ActivitySearchDeviceBinding;
import com.liesheng.haylou.event.BleDeviceEvent;
import com.liesheng.haylou.event.BleScanEvent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

/**
 * Date:2019/11/27
 * <p>
 * Author:wuzhiguang
 * <p>
 * Description:
 * 搜索设备
 */
public class SearchDeviceActivity extends BaseActivity<ActivitySearchDeviceBinding, BaseVm> {

    private List<BluetoothDevice> devices = new ArrayList<>();

    public static void startBy(BaseFunActivity activity) {
        Intent intent = new Intent(activity, SearchDeviceActivity.class);
        activity.startActivity(intent);
    }

    @Override
    protected ActivitySearchDeviceBinding getmBinding() {
        // 隐藏状态栏
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        mBinding = DataBindingUtil.inflate(getLayoutInflater(), R.layout.activity_search_device, null, false);
        return mBinding;
    }

    @Override
    protected BaseVm getViewModel() {
        return null;
    }

    @Override
    protected void initData() {
        setTitle(R.string.search_device);

        mBinding.tvRescan.performClick();

    }

    @Override
    protected boolean isRegistEventBus() {
        return true;
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void eventPost(BleScanEvent event) {
        //扫描结束
        mBinding.tvRescan.setVisibility(View.VISIBLE);
        mBinding.tvStatus.setText(devices.size() <= 0 ? R.string.scan_no_device : R.string.scan_over);
    }

    @Override
    protected void openBluetooth(boolean isOpen) {
        super.openBluetooth(isOpen);
        if (isOpen)
            mBinding.tvRescan.performClick();
    }

    BluetoothAdapter.LeScanCallback leScanCallback = new BluetoothAdapter.LeScanCallback() {
        @Override
        public void onLeScan(BluetoothDevice bluetoothDevice, int i, byte[] bytes) {

            if (devices.contains(bluetoothDevice)) return;

            BleDataInfo info = BleDataInfo.create(bytes, bluetoothDevice);

            if (info.canBeSearched()) {
                devices.add(bluetoothDevice);
                if (devices.size() == 1)
                    SelectDeviceActivity.startBy(SearchDeviceActivity.this, startScanTime);

                EventBus.getDefault().postSticky(new BleDeviceEvent(info));

            }
        }
    };

    long startScanTime;

    @Override
    public void onClickView(View view) {
        switch (view.getId()) {
            case R.id.tv_rescan:
                if (!isBluetoothOpen())
                    return;
                devices.clear();
                BleManager.getInstance().scanBle(leScanCallback);
                startScanTime = System.currentTimeMillis();
                mBinding.tvRescan.setVisibility(View.INVISIBLE);
                mBinding.tvStatus.setText(R.string.scan_device_ing);

                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        BleManager.getInstance().stopScan();
    }


}
